<?php
require_once 'init_fu.php';
require_once $site_config;
require_once $FU->dir->lib.'RL/UseScript.php';

$rh = new RL_UseScript();

$rh->useClass("RL_Debug");
$debug = new RL_Debug(/*min error level for halt*/3);

$config_dir = $FU->dir->config;
require_once "$config_dir/conf.d/db.php";

$rh->useClass("RL_DBAL");
$db = new RL_DBAL($debug);
$db->loadConfig($config);
$db->connect();

include_once 'includes/type.php';

$td = TypeDomain::instance();
$td->addLoader(new KarniTypeLoader("$config_dir/type.d"));
$td->addLoader(new DocTypeLoader("$config_dir/conf.d/doctype.php"));

// add default types
$td->setType('atom'        , new TAtom());
$td->setType('object'      , new TObject());
$td->setType('collection'  , new TCollection(array()));
$td->cloneType('string'    , 'atom');
$td->cloneType('integer'   , 'atom');
$td->cloneType('bool'      , 'atom');
$td->aliasType('boolean'   , 'bool');
$td->cloneType('text'      , 'atom');
$td->cloneType('datetime'  , 'atom');
$td->cloneType('timestamp' , 'atom');

require_once "$config_dir/conf.d/store.php";
require_once $config['code'].'/stores/'.$config['dbms'].'store.php';
$store_type = $config['dbms'].'store';

$store = new $store_type($config, $db, $td);

/**
 * начинаем забирать
 */
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

$rh->use_search = $self['use_search'];
$rh->search_in_comments = $self['search_in_comments'];

$rh->useClass('Search_Types');
$searchTypes = new Search_Types($rh, $store);

$types = $searchTypes->getListTypes();
foreach ($types as &$type) {
	$type = "'f{$type}'";
}
$commaTypes = join(',', $types);

$result = $store->query(
"select oid, type from {$config['db_prefix']}objects " .
" where is_deleted = 0 and type in ({$commaTypes}) order by oid");

while ( $item = $result->next() ) {
	$parser = $searchTypes->getTypeParser($item['type']);
	if ( false === $parser ) {
		print "Can`t find parser for '{$item['type']}' type.\n";
		continue;
	}

	$parser->linkToObject($item['oid']);
	$maked = $parser->makeSearchData($item['oid']);
	if ( !$maked ) {
		print "Can`t index item with '{$item['oid']}' oid.";
	}
}

$parser->cleanWords();

print "Done!\n";
